﻿using System;
using System.Linq;
using cludg.Domain.Environment;
using cludg.Repository.NHibernate.Sessions;
using NHibernate.Linq;

namespace cludg.Repository.Environment.NHibernate
{
	public class HistoryRepository : IHistoryRepository
	{
		protected readonly INHibernateSessionController SessionController;

		public HistoryRepository(INHibernateSessionController sessionController)
		{
			SessionController = sessionController;
		}

		public IQueryable<HistoryEntry> Get()
		{
			return SessionController.Current.Linq<HistoryEntry>(
				SessionController.Current.CreateCriteria<HistoryEntry>().SetCacheable(true));
		}


		public void Add(HistoryEntry historyEntry)
		{
			try
			{
				SessionController.Current.Save(historyEntry);
			}
			catch (Exception)
			{
				SessionController.Reset();
				throw;
			}
		}
	}
}
